import {
  IPublicTypeProjectSchema,
  IPublicTypeUtilsMap,
} from '@alilc/lowcode-types';

const defaultGlobalUtils: IPublicTypeUtilsMap = [
  {
    name: 'componentDidMount',
    type: 'function',
    content: {
      type: 'JSFunction',
      value: 'function() {\n\n}',
    },
  },
  {
    name: '__beforeRequest',
    type: 'function',
    content: {
      type: 'JSFunction',
      value: 'function(options) {\n return options;\n}',
    },
  },
  {
    name: '__afterRequest',
    type: 'function',
    content: {
      type: 'JSFunction',
      value: 'function(response) {\n return response;\n}',
    },
  },
];

export const mergeDefaultUtils = (schema: IPublicTypeProjectSchema) => {
  defaultGlobalUtils.forEach((_) => {
    schema.utils = schema.utils || [];
    const existIndex = schema.utils.findIndex((util) => util.name === _.name);
    if (existIndex < 0) {
      schema.utils.push(_);
    }
  });
  return schema;
};
